
const { doOrderPlugin } = require('./doOrderPlugin');
const { getLocalPluginByFileName } = require('../../../core/pluginSearchService/getLocalPluginByFile');
const { onEmit } = require('../../baseService/broadService/processor/broadcastProcessor');
const { asyncTopicConstants } = require('../../../common/constans/asyncTopicContans');
const EnvContext = require('../../../common/context/EnvContext');
const { getMainPreloadJsPath } = require('../../../common/utils/fileUtil')


exports.doSearchByFile = async function (request) {
    // decisionMethod: "string",     // 决策方法：根据正则，首写字母，文件类型等方法，对输入数据进行决策是否需要执行该命令
    // decisionDataType: "string",   // 决策数据类型：keyword，file，folder
    // decisionDataSource: "string", // 决策数据源：search(主窗口关键字搜索)，hover（鼠标选中），clipboard（粘贴板内存）
    // decisionData: "string",       // 决策数据：如果是关键字直接填字符串，如果是正则直接填正则表达式，如果是

    /*
    文件拖入入参
    {
        "input":null,
        "subInput":"plugin.json",
        "extInfo":{
            "path":"${path}"
        }
    }

    文件拖入同时搜索
    {
        "input":"${keyword}",
        "subInput":"plugin.json",
        "extInfo":{
            "path":"${path}"
        }
    }
    */
    searchLocalPlugin(request)
}


async function searchLocalPlugin(request) {
    const plugins = await getLocalPluginByFileName(request.subInput)
    const resultData = doOrderPlugin(plugins)
    const preloadPath = getMainPreloadJsPath()
    onEmit(asyncTopicConstants.onSearchPlugin, { cmdInfos: resultData, preloadPath: preloadPath })
}

